package com.kzj.mls.activity;

import android.os.Bundle;
import android.view.View;

import com.kzj.mls.R;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.card.other.ChangeNameCard;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class ChangeNameActivity extends GBaseActivity {
    private MaterialListView material_listview;
    private String activity;
    private ChangeNameCard changeNameCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
    }

    @Override
    protected void initDatas() {
        setTopTitle("昵称");
        setLeftImg(View.VISIBLE, R.drawable.back);
        setRightText(View.VISIBLE, "存储");
        activity = getIntent().getStringExtra("activity");
    }

    @Override
    protected void fillArrayInListView() {
        changeNameCard = new ChangeNameCard(this);
        changeNameCard.setName(App.getInstance().getUserBean().getUser_info().getStore_name());
        material_listview.add(changeNameCard);
    }

    private void TaskChangeName(String name) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_ChangeName, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_ChangeName, name), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        App.getInstance().showToast("修改成功！");
                        App.getInstance().getUserBean().getUser_info().setStore_name(result.getRetval().toString());
                        App.getInstance().setUserBean(App.getInstance().getUserBean());
                        EventBus.getDefault().post(App.getInstance().getUserBean());
                        ChangeNameActivity.this.finish();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    @Override
    public void doClickRightText() {
        super.doClickRightText();
        if (changeNameCard.getName()!=null&&!changeNameCard.getName().equals("")){
            TaskChangeName(changeNameCard.getName());
        }else{
            App.getInstance().showToast( "昵称不能为空哦");
        }
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
